<?php
class ModelAlbumAlbum extends Model {
	public function addAlbum($data) {
		$this->db->query("INSERT INTO " . DB_PREFIX . "album SET category_id = '" . (int)$data['category_id'] . "', sort_order = '" . (int)$data['sort_order'] . "', status = '" . (int)$data['status'] . "', created = NOW()");
	
		$album_id = $this->db->getLastId();
		
		if (isset($data['image'])) {
			$this->db->query("UPDATE " . DB_PREFIX . "album SET image = '" . $this->db->escape(html_entity_decode($data['image'], ENT_QUOTES, 'UTF-8')) . "' WHERE album_id = '" . (int)$album_id . "'");
		}
		
		foreach ($data['album_description'] as $language_id => $value) {
			$this->db->query("INSERT INTO " . DB_PREFIX . "album_description SET album_id = '" . (int)$album_id . "', language_id = '" . (int)$language_id . "', name = '" . $this->db->escape($value['name']) . "', description = '" . $this->db->escape($value['description']) . "'");
		}
		
		if (isset($data['album_image'])) {
			foreach ($data['album_image'] as $album_image) {
				$this->db->query("INSERT INTO " . DB_PREFIX . "album_image SET album_id = '" . (int)$album_id . "', image = '" . $this->db->escape(html_entity_decode($album_image['image'], ENT_QUOTES, 'UTF-8')) . "', sort_order = '" . (int)$album_image['sort_order'] . "'");
			}
		}
		
		if ($data['keyword']) {
			$this->db->query("INSERT INTO " . DB_PREFIX . "url_alias SET query = 'album_id=" . (int)$album_id . "', keyword = '" . $this->db->escape($data['keyword']) . "'");
		}
		
		$this->cache->delete('album');
	}
	
	public function editAlbum($album_id, $data) {
		$this->db->query("UPDATE " . DB_PREFIX . "album SET category_id = '" . (int)$data['category_id'] . "', sort_order = '" . (int)$data['sort_order'] . "', status = '" . (int)$data['status'] . "' WHERE album_id = '" . (int)$album_id . "'");
		
		if (isset($data['image'])) {
			$this->db->query("UPDATE " . DB_PREFIX . "album SET image = '" . $this->db->escape(html_entity_decode($data['image'], ENT_QUOTES, 'UTF-8')) . "' WHERE album_id = '" . (int)$album_id . "'");
		}

		$this->db->query("DELETE FROM " . DB_PREFIX . "album_description WHERE album_id = '" . (int)$album_id . "'");

		foreach ($data['album_description'] as $language_id => $value) {
			$this->db->query("INSERT INTO " . DB_PREFIX . "album_description SET album_id = '" . (int)$album_id . "', language_id = '" . (int)$language_id . "', name = '" . $this->db->escape($value['name']) . "', description = '" . $this->db->escape($value['description']) . "'");
		}
		
		$this->db->query("DELETE FROM " . DB_PREFIX . "album_image WHERE album_id = '" . (int)$album_id . "'");
		
		if (isset($data['album_image'])) {
			foreach ($data['album_image'] as $album_image) {
				$this->db->query("INSERT INTO " . DB_PREFIX . "album_image SET album_id = '" . (int)$album_id . "', image = '" . $this->db->escape(html_entity_decode($album_image['image'], ENT_QUOTES, 'UTF-8')) . "', sort_order = '" . (int)$album_image['sort_order'] . "'");
			}
		}
		
		$this->db->query("DELETE FROM " . DB_PREFIX . "url_alias WHERE query = 'album_id=" . (int)$album_id. "'");
		
		if ($data['keyword']) {
			$this->db->query("INSERT INTO " . DB_PREFIX . "url_alias SET query = 'album_id=" . (int)$album_id . "', keyword = '" . $this->db->escape($data['keyword']) . "'");
		}
		
		$this->cache->delete('album');
	}
	
	public function deleteAlbum($album_id) {
		$this->db->query("DELETE FROM " . DB_PREFIX . "album WHERE album_id = '" . (int)$album_id . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "album_description WHERE album_id = '" . (int)$album_id . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "url_alias WHERE query = 'album_id=" . (int)$album_id . "'");
		
		$query = $this->db->query("SELECT album_id FROM " . DB_PREFIX . "album WHERE category_id = '" . (int)$album_id . "'");

		foreach ($query->rows as $result) {
			$this->deletealbum($result['album_id']);
		}
		
		$this->cache->delete('album');
	} 

	public function getAlbum($album_id) {
		$query = $this->db->query("SELECT DISTINCT *, (SELECT keyword FROM " . DB_PREFIX . "url_alias WHERE query = 'album_id=" . (int)$album_id . "') AS keyword FROM " . DB_PREFIX . "album WHERE album_id = '" . (int)$album_id . "'");
		
		return $query->row;
	} 
	
	public function getAlbums() {
		$album_data = $this->cache->get('album.' . (int)$this->config->get('config_language_id'));
	
		if (!$album_data) {
			$album_data = array();
		
			$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "album c, " . DB_PREFIX . "album_description cd WHERE c.album_id = cd.album_id AND cd.language_id = '" . (int)$this->config->get('config_language_id') . "' ORDER BY c.sort_order, cd.name ASC");
			
			foreach ($query->rows as $result) {
				$album_data[] = array(
					'album_id' 	  => $result['album_id'],
					'name'        => $result['name'],
					'image'       => $result['image'],
					'feature'  	  => $result['feature'],
					'status'  	  => $result['status'],
					'created'  	  => $result['created'],
					'sort_order'  => $result['sort_order']
				);
			}	
	
			$this->cache->set('album.' . (int)$this->config->get('config_language_id'), $album_data);
		}
		
		return $album_data;
	}
		
	public function getAlbumDescriptions($album_id) {
		$album_description_data = array();
		
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "album_description WHERE album_id = '" . (int)$album_id . "'");
		
		foreach ($query->rows as $result) {
			$album_description_data[$result['language_id']] = array(
				'name'             => $result['name'],
				'description'      => $result['description']
			);
		}
		
		return $album_description_data;
	}	
	
	public function getTotalAlbums() {
      	$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "album");
		
		return $query->row['total'];
	}	

	public function getPath($album_id) {
		$query = $this->db->query("SELECT name FROM " . DB_PREFIX . "album c LEFT JOIN " . DB_PREFIX . "album_description cd ON (c.album_id = cd.album_id) WHERE c.album_id = '" . (int)$album_id . "' AND cd.language_id = '" . (int)$this->config->get('config_language_id') . "' ORDER BY c.sort_order, cd.name ASC");
		
		return $query->row['name'];
	}
	
	public function getAlbumImages($album_id) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "album_image WHERE album_id = '" . (int)$album_id . "'");
		
		return $query->rows;
	}
}
?>